package com.example.mytips.structure;

import com.intellij.openapi.startup.ProjectActivity;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.Constraints;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.components.BaseComponent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.startup.StartupActivity;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import kotlin.Unit;
import kotlin.coroutines.Continuation;

public class MyStartupActivity implements ProjectActivity {
    @Nullable
    @Override
    public Object execute(@NotNull Project project, @NotNull Continuation<? super Unit> continuation) {
        // 获取 ActionManager
        ActionManager actionManager = ActionManager.getInstance();

        DefaultActionGroup editorPopupMenu = (DefaultActionGroup) actionManager.getAction("EditorPopupMenu");
        if (editorPopupMenu == null) return null;

        // 获取自定义 Action
        CustomSaveAction customSaveAction = (CustomSaveAction) actionManager.getAction("com.example.mytips.structure.CustomSaveAction");
        if (customSaveAction == null) return null;

        // 移除自定义 Action（如果已存在）
        editorPopupMenu.remove(customSaveAction);

        // 将自定义 Action 添加到 EditorPopupMenu 的最上方
        editorPopupMenu.addAction(customSaveAction, Constraints.FIRST); // 0 表示最上方

        return null;
    }
}
